package com.asset.dispoal.auth;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import tech.mhuang.pacebox.core.check.CheckAssert;

@Configuration
@ConditionalOnProperty(prefix = "pacebox.filter", name = "enable", havingValue = "true")
@EnableConfigurationProperties(value = {AuthProperties.class})
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET)
@Slf4j
public class AuthConfiguration {

    private AuthProperties properties;

    public AuthConfiguration(AuthProperties properties) {
        CheckAssert.check(properties, "not found auth properties");
        this.properties = properties;
    }

    @Bean
    public FilterRegistrationBean<AuthFilter> authRegistrationBean() {
        AuthFilter authFilter = new AuthFilter();
        authFilter.setDefaultAuthTypeValue(properties.getFilterDefAuthType());
        FilterRegistrationBean<AuthFilter> registrationBean = new FilterRegistrationBean<>();
        registrationBean.setFilter(authFilter);
        registrationBean.setUrlPatterns(this.properties.getFilterIncludeUrl());
        registrationBean.setOrder(200);
        registrationBean.setName("AuthFilter");
        return registrationBean;
    }
}